var info = require('../infos/info')

// 用户登录
exports.userLogin = function(req,res) {
  var obj = req.body
  var sql = 'select * from user where num=? and password=? and type=?'
  var params = [obj.num, obj.pwd, obj.type]
  info.userLogin(sql, params, function (results) {
    if (results && results[0]!=undefined) {
      // 使用session记录用户登录信息
      req.session.info = results[0]
      res.send({
        pass:true,
        msg:'登录成功!'
      })
    }else {
      res.send({
        error:true,
        msg:'用户名或密码错误!'
      })
    }
  })
}

// 用户添加
exports.userAdd = function(req,res){
  var obj = req.body
  var params = []
  for (const key in obj) {
    params.push(obj[key])
  }
  var sql = 'insert into user values(?,?,?,?)'
  var sqln = 'select num from user where num=?'
  var promise = new Promise(function(resolve,reject){
    info.findOneInfo(sqln,[obj.num],function(results){
      if (results.count > 0) {
        reject({error:true,msg:'该用户可能已存在,添加失败!'})
      }else{
        resolve()
      }
    })
  })
  promise.then(function(){
    info.addInfo(sql,params,function(results){
      res.send(results)
    })
  },function(error){
    res.send(error)
  })
}
//用户更改
exports.userUpdate = function(req,res){
  var num = req.body['num']
  var password = req.body['password']
  var sql = 'update user set password=? where num=?'
  var params = [password,num]
  if (num == req.session.info['num']) {
    return res.send({affectedRows:0})
  }
  info.updateInfo(sql,params,function(results){
    res.send(results)
  })
}

// 用户信息添加
exports.userInfoAdd = function(req,res){
  var obj = req.body
  var params = []
  for (var key in obj) {
    params.push(obj[key])
  }
  var sql = 'insert into userinfo values(?,?,?,?,?,?,?)'
  var sqln = 'select num from userinfo where num=?'
  var promise = new Promise(function(resolve,reject){
    info.findOneInfo(sqln,[obj.num],function(results){
      if (results.count > 0) {
        reject({error:true,msg:'该用户可能已存在,添加失败!'})
      }else{
        resolve()
      }
    })
  })
  promise.then(function(){
    info.addInfo(sql,params,function(results){
      res.send(results)
    })
  },function(error){
    res.send(error)
  })
}

//用户信息更改
exports.userInfoUpdate = function(req,res){
  var obj = req.body
  if (obj.num == req.session.info['num']) {
    return res.send({affectedRows:0})
  }
  var params = [obj.phone,obj.address,obj.num]
  var sql = 'update userinfo set phone=?,address=? where num=?'
  info.updateInfo(sql,params,function(results){
    res.send(results)
  })
}

/**
 * 2.课程模块
 */
exports.courseInfoAdd = function(req,res){
  var obj = req.body
  var params = []
  for (var key in obj) {
    params.push(obj[key])
  }
  var sql = 'insert into courseinfo values(?,?,?)'
  var sqln = 'select cnum from courseinfo where cnum=?'
  var promise = new Promise(function(resolve,reject){
    info.findOneInfo(sqln,[obj.cnum],function(results){
      if (results.count > 0) {
        reject({error:true,msg:'该课程可能已存在,添加失败!'})
      }else{
        resolve()
      }
    })
  })
  promise.then(function(){
    info.addInfo(sql,params,function(results){
      res.send(results)
    })
  },function(error){
    res.send(error)
  })
}
exports.courseInfoUpdate = function(req,res){
  var obj = req.body
  var params = [obj.cname,obj.ctime,obj.cnum]
  var sql = 'update courseinfo set cname=?,ctime=? where cnum=?'
  info.updateInfo(sql,params,function(results){
    res.send(results)
  })
}

exports.scheduleInfoAdd = function(req,res){
  var obj = req.body
  var params = []
  for (var key in obj) {
    params.push(obj[key])
  }
  var sql = 'insert into scheduleinfo(classnos,cname,tname,weeks,week,jieci,location) values(?,?,?,?,?,?,?)'
  var sqln = 'select cname from scheduleinfo where cname=?'
  var promise = new Promise(function(resolve,reject){
    info.findOneInfo(sqln,[obj.cname],function(results){
      if (results.count > 0) {
        reject({error:true,msg:'该信息可能已存在,添加失败!'})
      }else{
        resolve()
      }
    })
  })
  promise.then(function(){
    info.addInfo(sql,params,function(results){
      res.send(results)
    })
  },function(error){
    res.send(error)
  })
}
exports.scheduleInfoUpdate = function(req,res){
  var obj = req.body
  var params = []
  for (var key in obj) {
    params.push(obj[key])
  }
  params.push(obj.tname)
  var sql = 'update scheduleinfo set classnos=?,cname=?,tname=?,weeks=?,week=?,jieci=?,location=? where tname=?'
  info.updateInfo(sql,params,function(results){
    res.send(results)
  })
}

/**
 * 机房模块
 */
exports.machineInfoAdd = function(req,res){
  var obj = req.body
  var params = []
  for (const key in obj) {
    params.push(obj[key])
  }
  var sql = 'insert into roominfo(rnum,location,state,renshu,devinfo) values(?,?,?,?,?)'
  var sqln = 'select * from roominfo where rnum=? and location=?'
  var promise = new Promise(function(resolve,reject){
    info.findOneInfo(sqln,[obj.rnum,obj.location],function(results){
      if (results.count > 0) {
        reject({error:true,msg:'该信息可能已存在,添加失败!'})
      }else{
        resolve()
      }
    })
  })
  promise.then(function(){
    info.addInfo(sql,params,function(results){
      res.send(results)
    })
  },function(error){
    res.send(error)
  })
}
exports.machineInfoUpdate = function(req,res){
  var obj = req.body
  var params = []
  for (var key in obj) {
    params.push(obj[key])
  }
  params.push(obj.rnum)
  var sql = 'update roominfo set rnum=?,location=?,state=?,renshu=?,devinfo=? where rnum=?'
  info.updateInfo(sql,params,function(results){
    res.send(results)
  })
}

exports.bookInfoAdd = function(req,res){

}
exports.bookInfoUpdate = function(req,res){
  
}